#include "sharedmemory.hpp"
#include <signal.h>

#define BUFF_SIZE 1024

int main()
{
    signal(17, SIG_IGN);
    My_Shared_Memory::SharedMemory myshm(FIFO_PATHNAME, 400, IPC_CREAT | IPC_EXCL | 0666);
    char *myarr = (char *)myshm.Get();
    int *server = (int *)myarr;
    int *client = (int *)(myarr + 4);
    char *filedata = myarr + 8;
    // std::cout << "地址" << &myarr << std::endl;
    //  std::cout << "内容" << myarr << std::endl;
    // myarr = "111";
    // std::cout << "地址" << &myarr << std::endl;
    // std::cout << "内容" << myarr << std::endl;
    pid_t pid = fork();
    if (pid == 0)
    {
        // std::cout << "地址" << &myarr << " pid: " << getpid() << std::endl;
        // std::cout << "内容" << myarr << " pid: " << getpid() << std::endl;
        //  myarr = "111";
        // std::cout << "地址" << &myarr << " pid: " << getpid() << std::endl;
        // std::cout << "内容" << myarr << " pid: " << getpid() << std::endl;
        // std::cout << "地址" << &server << " pid: " << getpid() << std::endl;
        // std::cout << "地址" << &client << " pid: " << getpid() << std::endl;
        // std::cout << "地址" << &filedata << " pid: " << getpid() << std::endl;
        while (1)
        {
            // 触发传输
            if (*server != 0 && *client == 0)
            {
                char buffer[10][10] = {" ", "1a", "2b", "3c", "4d", "close"};
                if (strcmp(buffer[*server], "close") == 0)
                {
                    *client = *server;
                    break;
                }
                else
                {
                    filedata = buffer[*server];
                    std::cout << filedata << std::endl;
                    *client = *server;
                }
            }
            // 传输接收确认
            if (*server == 0 && *client != 0)
            {
                *client = 0;
            }
            sleep(1);
        }
        std::cout << "child end pid: " << getpid() << std::endl;
        exit(0);
    }
    else if (pid > 0)
    {
        // std::cout << "地址" << &myarr << " pid: " << getpid() << std::endl;
        // std::cout << "内容" << myarr << " pid: " << getpid() << std::endl;
        // myarr = "111";
        // std::cout << "地址" << &myarr << " pid: " << getpid() << std::endl;
        // std::cout << "内容" << myarr << " pid: " << getpid() << std::endl;
        // std::cout << "地址" << &server << " pid: " << getpid() << std::endl;
        // std::cout << "地址" << &client << " pid: " << getpid() << std::endl;
        // std::cout << "地址" << &filedata << " pid: " << getpid() << std::endl;
        while (1)
        {
            // std::cout << *client << std::endl;
            // std::cout << *server << std::endl;
            std::cout << "输入->";
            std::cin >> (*server);
            while (1)
            {
                if (*client == *server)
                {
                    if (*client == 5)
                    {
                        goto endparent;
                    }
                    *server = 0;
                    break;
                }
            }
            sleep(1);
        }
    endparent:
        std::cout << "parent end pid: " << getpid() << std::endl;
    }

    return 0;
}
